package tk.uodrad.apex.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import tk.uodrad.apex.project.ex.ServiceException;
import tk.uodrad.apex.project.mapper.ParkingUseMapper;
import tk.uodrad.apex.project.pojo.dto.ParkingUseAddNewDTO;
import tk.uodrad.apex.project.pojo.dto.ParkingUseUpdateDTO;
import tk.uodrad.apex.project.pojo.entity.ParkingUse;
import tk.uodrad.apex.project.pojo.vo.ParkingUseListVO;
import tk.uodrad.apex.project.service.ParkingUseService;
import org.springframework.stereotype.Service;
import tk.uodrad.apex.project.web.ServiceCode;

import java.util.Date;

/**
* @author uodrad
* @description 针对表【parking_use_management】的数据库操作Service实现
* @createDate 2023-04-08 12:54:27
*/
@Service
public class ParkingUseServiceImpl extends ServiceImpl<ParkingUseMapper, ParkingUse>
    implements ParkingUseService {
    @Autowired
    private ParkingUseMapper parkingUseMapper;

    /**
     * 添加单个车位使用
     * @param parkingUseAddNewDTO 添加车位使用DTO
     */
    @Override
    public void save(ParkingUseAddNewDTO parkingUseAddNewDTO) {
        //新建一个ParkingUse对象并将传入的DTO复制给ParkingUse
        ParkingUse parkingUse = new ParkingUse();
        BeanUtils.copyProperties(parkingUseAddNewDTO, parkingUse);
        //parkingUse
        parkingUse.setGmtCreate(new Date());
        //创建MyBatis-plus提供的Wrappers类下的query()方法
        QueryWrapper<ParkingUse> wrapper = Wrappers.query();
        //用eq方法用于判断数据库中是否有license_plate等于当前ParkingUse的license_plate（其他模块一般根据实际情况判断数据是否存在）
        wrapper.eq("license_plate", parkingUse.getLicensePlate());
        ParkingUse selectOne = parkingUseMapper.selectOne(wrapper);
        if (selectOne != null) {
            String message = "新增数据失败，当前数据已存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        //插入数据，并判断是否插入成功
        int rows = parkingUseMapper.insert(parkingUse);
        if (rows != 1) {
            String message = "服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }
    /**
     * 根据ID修改车位使用
     * @param parkingUseUpdateDTO 修改车位使用DTO
     */
    @Override
    public void updateById(ParkingUseUpdateDTO parkingUseUpdateDTO) {
        //根据传入的DTO中的ID查询数据库中是否存在该数据
        ParkingUse parkingUseById = parkingUseMapper.selectById(parkingUseUpdateDTO.getId());
        if (parkingUseById == null) {
            String message = "修改数据失败，当前数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        //创建一个ParkingUse对象，将传入的DTO复制给ParkingUse对象
        ParkingUse parkingUse = new ParkingUse();
        BeanUtils.copyProperties(parkingUseUpdateDTO, parkingUse);
        //给parkingUse对象赋值创建时间
        parkingUse.setGmtModified(new Date());
        //更新数据，并判断是否更新成功
        int rows = parkingUseMapper.updateById(parkingUse);
        if (rows != 1) {
            String message = "服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }
    /**
     * 分页查询车位使用列表
     * @param pageNum 第几页
     * @param pageSize 每页多少条数据
     * @return IPage<ParkingUse> 分页对象
     */
    @Override
    public IPage<ParkingUseListVO> list(Integer pageNum, Integer pageSize, String communityCode) {
        Page<ParkingUseListVO> page = new Page<>(pageNum, pageSize);
        return parkingUseMapper.selectPageVo(page, communityCode);
    }
}




